﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using ZFB.Common;
using ZFB.DTO;
using ZFB.IServices;

namespace ZFB.WebAPI.Controllers
{
    /// <summary>
    /// 管理员
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AdminUserController : ControllerBase
    {
        private readonly IAdminUserServices _services;

        public AdminUserController(IAdminUserServices services)
        {
            _services = services;
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="addUserDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddAdminUser(AddAdminUserDTO addUserDTO)
        {
            return await _services.AddNewUserAsync(addUserDTO.name, addUserDTO.phoneNum, addUserDTO.password, addUserDTO.email, addUserDTO.cityid);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Login(LoginDTO loginDTO) 
        {
            return await _services.CheckLoginAsync(loginDTO.phoneNum, loginDTO.password);
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetAdminUser()
        {
            return await _services.GetAllAsync();
        }
        /// <summary>
        /// 获取城市下所有用户
        /// </summary>
        /// <param name="cityid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetAllByCityId(long? cityid)
        {
            return await _services.GetAllByCityIdAsync(cityid);
        }
        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="adminUserId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> MarkDelete(long adminUserId) 
        {
            return await _services.MarkDeleteAsync(adminUserId);
        }
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="phoneNum"></param>
        /// <param name="email"></param>
        /// <param name="cityid"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdateUser(long id, string name, string phoneNum,string email,long?cityid)
        {
            return await _services.UpdateUserAsync( id,name,phoneNum,email,cityid);

        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="phoneNum"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdatePswAsync(string phoneNum, string password) 
        {
            return await _services.UpdatePswAsync(phoneNum,password);
        }
        /// <summary>
        /// 手机号获取用户
        /// </summary>
        /// <param name="phonenum"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetAllByPhone(string phonenum)
        {
            return await _services.GetByPhoneAsync(phonenum);
        }
        /// <summary>
        /// Id获取用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetAllById(long id)
        {
            return await _services.GetByIdAsync(id);
        }
        /// <summary>
        /// 检查权限
        /// </summary>
        /// <param name="id"></param>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> CheckPermission(long id,string permissionName)
        {
            return await _services.HasPermissionAsync(id,permissionName);
        }

    }
}
